/*

    设计一个英雄的结构体，包括成员姓名，年龄，性别;创建结构体数组，数组中存放5名英雄。
    通过冒泡排序的算法，将数组中的英雄按照年龄进行升序排序，最终打印排序后的结果。

    五名英雄如下
        {"刘备",23,"男"},
        {"关羽",22,"男"},
        {"张飞",20,"男"},
        {"赵云",21,"男"},
        {"貂蝉",19,"女"},
*/

#include <iostream>

struct hero
{
    std::string name;
    int age;
    std::string gender;
};

void print_heros(const hero *heroes, int len)
{
    // title
    std::cout << "No.\tName\tAge\tGender" << std::endl;
    for (int i = 0; i < len; i++)
    {
        std::cout << i + 1 << "\t" << heroes[i].name << "\t" << heroes[i].age << "\t" << heroes[i].gender << std::endl;
    }
}

void sort(hero *heroes, int len)
{
    for (int i = len - 1; i >= 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (heroes[j + 1].age < heroes[j].age)
            {
                hero temp = heroes[j];
                heroes[j] = heroes[j + 1];
                heroes[j + 1] = temp;
            }
        }
    }
}

int main(int argc, char const *argv[])
{
    const int num_hero = 5;
    hero heroes[num_hero] = {
        {"刘备", 23, "男"},
        {"关羽", 22, "男"},
        {"张飞", 20, "男"},
        {"赵云", 21, "男"},
        {"貂蝉", 19, "女"}};
    std::cout << "排序前:" << std::endl;
    print_heros(heroes, num_hero);

    sort(heroes, num_hero);

    std::cout << "排序后:" << std::endl;
    print_heros(heroes, num_hero);

    return 0;
}
